import 'dart:io';
import 'package:flutter_model_package/base/api_response.dart';
import 'package:flutter_model_package/student.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

import 'Branch.dart';

const String _keyTableName = 'Employee';
const String keyObjectId = 'objectId';
const String keyCreatedAt = 'createdAt';
const String keyUpdatedAt = 'updatedAt';
const String keyNumber = 'number';
const String keyDisplayName = 'displayName';
const String keyEmail = 'email';
const String keyPhone = 'phone';
const String keyTitle = 'title';
const String keyGender = 'gender';
const String keyAvatar = 'avatar';
const String keyIdNo = 'idNo';
const String keyIdCopy = 'idCopy';
const String keyJoinAt = 'joinAt';
const String keyBirthday = 'birthday';
const String keyAddress = 'address';
const String keyAccountExpires = 'accountExpires';
const String keyUserAccountControl = 'userAccountControl';
const String keyOldMySQLID = 'oldMySQLID';
const String keyBranch = 'branch';

class Employee extends ParseObject implements ParseCloneable {
  Employee() : super(_keyTableName);

  Employee.clone() : this();

  @override
  Employee clone(Map<String, dynamic> map) => Employee.clone()..fromJson(map);

  @override
  Employee fromJson(Map<String, dynamic> objectData) {
    super.fromJson(objectData);

    if (objectData.containsKey(keyBranch)) {
      branch = Branch.clone().fromJson(objectData[keyBranch]);
    }
    return this;
  }

  String get objectId => get<String>(keyObjectId);

  set objectId(String objectId) => set<String>(keyObjectId, objectId);

  DateTime get createdAt => get<DateTime>(keyCreatedAt);

  set createdAt(DateTime createdAt) => set<DateTime>(keyCreatedAt, createdAt);

  DateTime get updatedAt => get<DateTime>(keyUpdatedAt);

  set updatedAt(DateTime updatedAt) => set<DateTime>(keyUpdatedAt, updatedAt);

  String get number => get<String>(keyNumber);

  set number(String number) => set<String>(keyNumber, number);

  String get displayName => get<String>(keyDisplayName);

  set displayName(String displayName) =>
      set<String>(keyDisplayName, displayName);

  String get email => get<String>(keyEmail);

  set email(String email) => set<String>(keyEmail, email);

  String get phone => get<String>(keyPhone);

  set phone(String phone) => set<String>(keyPhone, phone);

  String get title => get<String>(keyTitle);

  set title(String title) => set<String>(keyTitle, title);

  String get gender => get<String>(keyGender);

  set gender(String gender) => set<String>(keyGender, gender);

  File get avatar => get<File>(keyAvatar);

  set avatar(File avatar) => set<File>(keyAvatar, avatar);

  String get idNo => get<String>(keyIdNo);

  set idNo(String idNo) => set<String>(keyIdNo, idNo);

  String get idCopy => get<String>(keyIdCopy);

  set idCopy(String idCopy) => set<String>(keyIdCopy, idCopy);

  String get joinAt => get<String>(keyJoinAt);

  set joinAt(String joinAt) => set<String>(keyJoinAt, joinAt);

  DateTime get birthday => get<DateTime>(keyBirthday);

  set birthday(DateTime birthday) => set<DateTime>(keyBirthday, birthday);

  String get address => get<String>(keyAddress);

  set address(String address) => set<String>(keyAddress, address);

  String get accountExpires => get<String>(keyAccountExpires);

  set accountExpires(String accountExpires) =>
      set<String>(keyAccountExpires, accountExpires);

  num get userAccountControl => get<num>(keyUserAccountControl);

  set userAccountControl(num userAccountControl) =>
      set<num>(keyUserAccountControl, userAccountControl);

  num get oldMySQLID => get<num>(keyOldMySQLID);

  set oldMySQLID(num oldMySQLID) => set<num>(keyOldMySQLID, oldMySQLID);

  Branch get branch => get<Branch>(keyBranch);

  set branch(Branch branch) => set<Branch>(keyBranch, branch);

  Iterable<Branch> get branchs => get<List<dynamic>>("branches").map((e) => e);

  /// 查询员工所有所在学校
  QueryBuilder<Branch> branchers(Employee emloyee) {
    // var q = query || QueryBuilder(Branch());
    var q = QueryBuilder(Branch());
    var branch = emloyee.get("branch");
    List<String> ids = [];
    if (branch != null) {
      ids.add(branch["objectId"]);
    }

    /// 取出branches
    var branches = emloyee.get("branches");
    if (branches != null && branches.length != 0) {
      branches.map((e) => ids.add(e["objectId"])).toList();
    }
    if (ids.length != 0) q..whereContainedIn("objectId", ids);
    return q;
  }

  QueryBuilder<Employee> forIdEmployee(String id) {
    return QueryBuilder(this)..whereEqualTo("objectId", id);
  }

  /// 查询员工所有学生
  Future<ParseResponse> students(Employee employee) async {
    var branch = this.get("branch");
    var branches = this.get("branches");
    List all = [];
    all.add(branch);
    branches.map((e)=>all.add(e)).toList();
    var q = QueryBuilder(Student())..whereContainedIn("branch", all);
    ParseResponse rep = await q.query();
    return rep;
  }
Future<ApiResponse> gg()async{
    var q = QueryBuilder(Employee());
    return getApiResponse<Employee>(await q.query());
}
  QueryBuilder<Student> studentss(){
    var branch = this.get("branch");
    var branches = this.get("branches");
    List all = [];
    all.add(branch);
    branches.map((e)=>all.add(e)).toList();
    return QueryBuilder(Student())..whereContainedIn("branch", all);
  }
}
